home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / i18n / main.cpp.z / main.cpp
C/C++ Source or Header  |  2002-04-08  |  4KB  |  172 lines

  1. /****************************************************************************
  2. ** $Id:  qt/main.cpp   3.0.3   edited Dec 6 13:41 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include <qapplication.h>
  12. #include <qtranslator.h>
  13. #include <qfileinfo.h>
  14. #include <qmessagebox.h>
  15. #include <qcheckbox.h>
  16. #include <qvbox.h>
  17. #include <qlayout.h>
  18. #include <qbuttongroup.h>
  19. #include <qpushbutton.h>
  20. #include <qsignalmapper.h>
  21. #include <qtextcodec.h>
  22. #include <stdlib.h>
  23.  
  24. #if defined(Q_OS_UNIX)
  25. #include <unistd.h>
  26. #endif
  27.  
  28. #include "mywidget.h"
  29.  
  30. //#define USE_I18N_FONT
  31.  
  32. class QVDialog : public QDialog {
  33. public:
  34.     QVDialog(QWidget *parent=0, const char *name=0, bool modal=FALSE,
  35.              WFlags f=0) : QDialog(parent,name,modal,f)
  36.     {
  37.     QVBoxLayout* vb = new QVBoxLayout(this,8);
  38.     vb->setAutoAdd(TRUE);
  39.     hb = 0;
  40.     sm = new QSignalMapper(this);
  41.     connect(sm,SIGNAL(mapped(int)),this,SLOT(done(int)));
  42.     }
  43.     void addButtons( const QString& cancel=QString::null,
  44.             const QString& ok=QString::null,
  45.             const QString& mid1=QString::null,
  46.             const QString& mid2=QString::null,
  47.             const QString& mid3=QString::null)
  48.     {
  49.     addButton(ok.isNull() ? tr("OK") : ok, 1);
  50.     if ( !mid1.isNull() ) addButton(mid1,2);
  51.     if ( !mid2.isNull() ) addButton(mid2,3);
  52.     if ( !mid3.isNull() ) addButton(mid3,4);
  53.     addButton(cancel.isNull() ? tr("Cancel") : cancel, 0);
  54.     }
  55.  
  56.     void addButton( const QString& text, int result )
  57.     {
  58.     if ( !hb )
  59.         hb = new QHBox(this);
  60.     QPushButton *c = new QPushButton(text, hb);
  61.     sm->setMapping(c,result);
  62.     connect(c,SIGNAL(clicked()),sm,SLOT(map()));
  63.     }
  64.  
  65. private:
  66.     QSignalMapper *sm;
  67.     QHBox *hb;
  68. };
  69.  
  70. MyWidget* showLang(QString lang)
  71. {
  72.  
  73.     static QTranslator *translator = 0;
  74.  
  75.     qApp->setPalette(QPalette(QColor(220-rand()%64,220-rand()%64,220-rand()%64)));
  76.  
  77.     lang = "mywidget_" + lang + ".qm";
  78.     QFileInfo fi( lang );
  79.  
  80.     if ( !fi.exists() ) {
  81.     QMessageBox::warning( 0, "File error",
  82.                   QString("Cannot find translation for language: "+lang+
  83.                       "\n(try eg. 'de', 'ko' or 'no')") );
  84.     return 0;
  85.     }
  86.     if ( translator ) {
  87.     qApp->removeTranslator( translator );
  88.     delete translator;
  89.     }
  90.     translator = new QTranslator( 0 );
  91.     translator->load( lang, "." );
  92.     qApp->installTranslator( translator );
  93.     MyWidget *m = new MyWidget;
  94.     m->setCaption("Qt Example - i18n - " + m->caption() );
  95.     return m;
  96. }
  97.  
  98. int main( int argc, char** argv )
  99. {
  100.     QApplication app( argc, argv );
  101.  
  102.     const char* qm[]=
  103.     { "cs", "de", "el", "en", "eo", "fr", "it", "jp", "ko", "no", "ru", "zh", 0 };
  104.  
  105. #if defined(Q_OS_UNIX)
  106.     srand( getpid() << 2 );
  107. #endif
  108.  
  109.     QString lang;
  110.     if ( argc == 2 )
  111.         lang = argv[1];
  112.  
  113.     if ( argc != 2 || lang == "all" ) {
  114.     QVDialog dlg(0,0,TRUE);
  115.     QCheckBox* qmb[sizeof(qm)/sizeof(qm[0])];
  116.     int r;
  117.     if ( lang == "all" ) {
  118.         r = 2;
  119.     } else {
  120.         QButtonGroup *bg = new QButtonGroup(4,Qt::Vertical,"Choose Locales",&dlg);
  121.         QString loc = QTextCodec::locale();
  122.         for ( int i=0; qm[i]; i++ ) {
  123.         qmb[i] = new QCheckBox((const char*)qm[i],bg);
  124.         qmb[i]->setChecked( loc == qm[i] );
  125.         }
  126.         dlg.addButtons("Cancel","OK","All");
  127.         r = dlg.exec();
  128.     }
  129.     if ( r ) {
  130.         bool tight = qApp->desktop()->screen()->width() < 1024;
  131.         int x=5;
  132.         int y=25;
  133.         for ( int i=0; qm[i]; i++ ) {
  134.         if ( r == 2 || qmb[i]->isChecked() ) {
  135.             MyWidget* w = showLang((const char*)qm[i]);
  136.  
  137.             if( w == 0 ) exit( 0 );
  138.             QObject::connect(w, SIGNAL(closed()), qApp, SLOT(quit()));
  139.             w->setGeometry(x,y,197,356);
  140.             w->show();
  141.             if ( tight ) {
  142.             x += 8;
  143.             y += 8;
  144.             } else {
  145.             x += 205;
  146.             if ( x > 1000 ) {
  147.                 x = 5;
  148.                 y += 384;
  149.             }
  150.             }
  151.         }
  152.         }
  153.     } else {
  154.             exit( 0 );
  155.         }
  156.     } else {
  157.     QString lang = argv[1];
  158.     QWidget* m = showLang(lang);
  159.     app.setMainWidget( m );
  160.     m->setCaption("Qt Example - i18n");
  161.     m->show();
  162.     }
  163.  
  164. #ifdef USE_I18N_FONT
  165.     memorymanager->savePrerenderedFont(font.handle(),FALSE);
  166. #endif
  167.  
  168.     // While we run "all", kill them all
  169.     return app.exec();
  170.  
  171. }
  172.